/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.utfpr.servlet;

import br.edu.utfpr.dao.Dao;
import br.edu.utfpr.model.Circuito;
import br.edu.utfpr.model.Pais;
import br.edu.utfpr.model.Prova;
import br.edu.utfpr.utils.Data;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author andre
 */
@WebServlet(name = "ProvaServlet", urlPatterns = {"/ProvaServlet"})
public class ProvaServlet extends HttpServlet {

    private String op;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        op = request.getParameter("op");

        if (op.equals("find")) {
            Dao<Prova> dao = new Dao<Prova>(Prova.class);
            Prova prova = dao.obter(Integer.parseInt(request.getParameter("id")));
            if (prova != null) {
                request.getSession().setAttribute("prova", prova);
                response.sendRedirect("edProva.jsp");
            } else {
                request.getSession().setAttribute("msg", "Prova inexistente");
                response.sendRedirect("prova.jsp");
            }
        } else if (op.equals("remove")) {
            Dao<Prova> dao = new Dao<Prova>(Prova.class);
            dao.remover(Integer.parseInt(request.getParameter("id")));
            response.sendRedirect("prova.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        op = request.getParameter("op");
        String gp = request.getParameter("gp");
        Date dataC = Data.StringToDate(request.getParameter("data"));
        int numVoltas = Integer.parseInt(request.getParameter("numVoltas"));
        int extensao = Integer.parseInt(request.getParameter("extensao"));
        int circuitoId = Integer.parseInt(request.getParameter("circuito_id"));

        Dao<Prova> dao = new Dao<Prova>(Prova.class);

        if (op.equals("insert")) {
            Prova p = new Prova();
            p.setGp(gp);
            p.setDataC(dataC);
            p.setNumVoltas(numVoltas);
            p.setExtensao(extensao);
            p.setCircuito(new Dao<Circuito>(Circuito.class).obter(circuitoId));
            p.setStatus(request.getParameter("status") != null);
            dao.inserir(p);
        } else if (op.equals("update")) {
            Prova p = (Prova) request.getSession().getAttribute("prova");
            p.setGp(gp);
            p.setDataC(dataC);
            p.setNumVoltas(numVoltas);
            p.setExtensao(extensao);
            p.setCircuito(new Dao<Circuito>(Circuito.class).obter(circuitoId));
            p.setStatus(request.getParameter("status") != null);
            dao.atualizar(p);
            request.getSession().removeAttribute("prova");
        }
        response.sendRedirect("prova.jsp");
    }
}